package com.threeox.commonlibrary.ui.view.pullrefresh.headfooter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IHeaderFooter;
import com.threeox.utillibrary.util.res.LayoutParamsUtils;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 *
 * @ClassName: HeaderView
 *
 * @Description: Todo(下拉的View)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/22 9:33
 *
 * @version 1.0
 */
public class HeaderView extends LinearLayout implements IHeaderFooter {

    private View mHeadView;
    private ImageView mHeaderArrow; // 头布局的剪头
    private ProgressBar mHeaderCircle; // 头布局的进度条
    private TextView mHeaderState; // 头布局的状态
    private ImageView mHeaderStateImg;// 头部据的状态图片

    private Animation mUpAnimation; // 向上旋转的动画
    private Animation mDownAnimation; // 向下旋转的动画

    public HeaderView(Context context) {
        this(context, null);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHeadView = LayoutUtils.inflate(context, R.layout.view_header_view);
        mHeadView.setLayoutParams(LayoutParamsUtils.newInstance(context).getLinearLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        this.addView(mHeadView);
        initView();
        initAnimation();
    }

    private void initView() {
        mHeaderArrow = (ImageView) findViewById(R.id.id_header_arrow);
        mHeaderCircle = (ProgressBar) findViewById(R.id.id_header_circle);
        mHeaderState = (TextView) findViewById(R.id.id_header_state);
        mHeaderStateImg = (ImageView) findViewById(R.id.id_header_state_img);
    }

    /**
     * 初始化动画
     */
    private void initAnimation() {
        mUpAnimation = new RotateAnimation(0f, -180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mUpAnimation.setDuration(500);
        mUpAnimation.setFillAfter(true); // 动画结束后, 停留在结束的位置上
        mDownAnimation = new RotateAnimation(-180f, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mDownAnimation.setDuration(500);
        mDownAnimation.setFillAfter(true); // 动画结束后, 停留在结束的位置上
    }

    @Override
    public void init(String msg) {
        // 下拉布局初始状态
        mHeaderStateImg.setVisibility(View.GONE);
        mHeaderCircle.setVisibility(View.GONE);
        mHeaderArrow.clearAnimation();
        mHeaderArrow.setVisibility(View.VISIBLE);
        mHeaderState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.pull_to_refresh) : msg));
    }

    @Override
    public void loading(String msg) {
        // 正在刷新状态
        mHeaderArrow.clearAnimation();
        mHeaderArrow.setVisibility(View.GONE);
        mHeaderCircle.setVisibility(View.VISIBLE);
        mHeaderState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.refreshing) : msg));
    }

    @Override
    public void release(String msg) {
        // 释放刷新状态
        mHeaderArrow.startAnimation(mUpAnimation); // 执行向上旋转
        mHeaderState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.release_to_refresh) : msg));
    }

    @Override
    public void loadFailed(String msg) {
        // 刷新失败
        mHeaderCircle.setVisibility(View.INVISIBLE);
        mHeaderStateImg.setVisibility(View.VISIBLE);
        mHeaderStateImg.setBackgroundResource(R.mipmap.ic_load_failed);
        mHeaderState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.load_fail) : msg));
    }

    @Override
    public void loadSucceed(String msg) {
        // 刷新成功
        mHeaderCircle.setVisibility(View.INVISIBLE);
        mHeaderStateImg.setVisibility(View.VISIBLE);
        mHeaderStateImg.setBackgroundResource(R.mipmap.ic_load_succeed);
        mHeaderState.setText((TextUtils.isEmpty(msg) ? getContext().getString(R.string.refresh_succeed) : msg));
    }
}
